package org.xiaoyu.rpc.remoting.transport.cllient.future;


import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created By Have
 * 2021/7/16 12:05
 */
public class NamedThreadFactory implements ThreadFactory {
    private static final AtomicInteger ato = new AtomicInteger(0);

    protected final String prefix;
    protected final boolean daemon;
    protected final ThreadGroup mGroup;

    public NamedThreadFactory(String prefix, boolean daemon) {
        this.prefix = prefix;
        this.daemon = daemon;
        SecurityManager s = System.getSecurityManager();
        mGroup = (s == null) ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
    }

    public NamedThreadFactory() {
        this("pool", false);
    }

    @Override
    public Thread newThread(Runnable r) {
        int number = ato.getAndIncrement();
        Thread thread = new Thread(mGroup, r, prefix + "-thread-" + number, 0);
        thread.setDaemon(this.daemon);
        return thread;
    }
}
